<template>
  <div class="tree-box">
    <el-row>
      <el-col :span="20"> {{ deDate.name }}</el-col>
      <el-col :span="4" style="text-align: right" class="col-4">
        <el-row class="row-el">
          <el-col :span="12" style="height: 26px">{{ deDate.manager }}</el-col>
          <el-col :span="12">
            <el-dropdown>
              <span> 操作<i class="el-icon-arrow-down" /> </span>
              <!-- 下拉菜单 -->
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="addDepartment(deDate.id)"
                  >添加子部门</el-dropdown-item
                >
                <el-dropdown-item
                  v-if="treeFlag"
                  @click.native="editDepartment(deDate)"
                  >编辑部门</el-dropdown-item
                >
                <el-dropdown-item
                  v-if="treeFlag"
                  @click.native="removeDepartment(deDate.id)"
                  >删除部门</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown></el-col
          >
        </el-row></el-col
      >
    </el-row>
    <!-- 退出的表单对话框 -->
    <AddDia
      v-if="dialogFormVisible"
      :id="id"
      :manager="manager"
      :dialog-form-visible.sync="dialogFormVisible"
      :depart-item="departItem"
      :is-edit="isEdit"
      @success="$emit('success')"
    />
  </div>
</template>

<script>
import AddDia from '../../views/departments/components/addDia'
import { getDepartManagerApi, delDepartmentApi } from '@/api/departments'
export default {
  name: 'Depart',
  components: { AddDia },
  props: {
    deDate: {
      type: Object,
      default: () => {}
    },
    treeFlag: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formLabelWidth: '120px',
      dialogFormVisible: false,
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      manager: [],
      id: '',
      isEdit: false,
      departItem: {}
    }
  },
  computed: {},
  created() {},
  methods: {
    addDepartment(id) {
      this.isEdit = false
      this.dialogFormVisible = true
      this.id = id
      this.getUserSimple() // 将部门负责人的数据传给子组件
    },
    async getUserSimple() {
      const res = await getDepartManagerApi()
      this.manager = res.data
    },
    editDepartment(data) {
      this.departItem = data
      this.dialogFormVisible = true
      this.isEdit = true
      console.log(data)
    },
    removeDepartment(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delDepartmentApi(id)
          this.$emit('success')
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  flex: 1;
}
</style>
